package com.xingxue.logistics.common.vo;

import org.apache.commons.lang3.builder.MultilineRecursiveToStringStyle;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.LinkedList;
import java.util.List;

/**
 * eayui tree组件需要的数据模型。用于构建树形UI。
 * @author Kris at 2018/9/5 17:44
 */
public class TreeNode {
    /**
     * 节点ID
     */
    private Integer id;
    /**
     * 上级节点ID
     */
    private Integer parenId;
    /**
     * 展示的文本内容
     */
    private String text;
    /**
     * 节点url（菜单使用）
     */
    private String linkUrl;
    /**
     * 是否选中当前节点
     */
    private Boolean checked;
    /**
     * 下级节点
     */
    private List<TreeNode> children;

    public static List<TreeNode> buildTree(List<TreeNode> src) {
        //找到root节点。
        List<TreeNode> rootNodeList = findRoot(src);
        //循环root节点去装children
        for (TreeNode node : rootNodeList) {
            matchChildren(node, src);
        }
        System.out.println(rootNodeList);
        return rootNodeList;
    }

    public static void matchChildren(TreeNode parent, List<TreeNode> src) {
        for (TreeNode node : src) {
            if (parent.getId().equals(node.getParenId())) {
                if (parent.getChildren() == null) {
                    parent.setChildren(new LinkedList<TreeNode>());
                }
                parent.getChildren().add(node);
                matchChildren(node, src);
            }
        }
    }

    /**
     * 查找根节点
     *
     * @param src 源
     */
    private static List<TreeNode> findRoot(List<TreeNode> src) {
        List<TreeNode> root = new LinkedList<>();
        for (TreeNode node : src) {
            //上级节点为null或0就是根节点
            if (node.getParenId() == null || node.getParenId().equals(0)) {
                root.add(node);
            }
        }
        return root;
    }

    public TreeNode() {
    }

    public TreeNode(Integer id, Integer parenId, String text, String linkUrl, Boolean checked, List<TreeNode> children) {
        this.id = id;
        this.parenId = parenId;
        this.text = text;
        this.linkUrl = linkUrl;
        this.checked = checked;
        this.children = children;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getParenId() {
        return parenId;
    }

    public void setParenId(Integer parenId) {
        this.parenId = parenId;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getLinkUrl() {
        return linkUrl;
    }

    public void setLinkUrl(String linkUrl) {
        this.linkUrl = linkUrl;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

    public Boolean getChecked() {
        return checked;
    }

    public void setChecked(Boolean checked) {
        this.checked = checked;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, new MultilineRecursiveToStringStyle())
                .append("id", id)
                .append("parenId", parenId)
                .append("text", text)
                .append("linkUrl", linkUrl)
                .append("checked", checked)
                .append("children", children)
                .toString();
    }
}



